import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:test_flutter/util/NavigatorUtils.dart';

///导航类
///1、跳转和返回 √
///   a：静态路由：Navigator.pushNamed
///   b：动态路由：Navigator.push
///2、携带参数和返回参数（返回参数只能传一个参数，没法传多个） √
///   a：普通传递，通过构造函数传递
///   b：argument传递：只适合静态路由
///3、路由跳转动画
///4、主流的路由框架
///5、类似android中的启动模式
///6、有没有类似activity和fragment的嵌套方式
class MyNavigatorPage extends StatefulWidget {
  final String argument;

  MyNavigatorPage({this.argument = '导航页面'});

  @override
  State<StatefulWidget> createState() => NavigatorState();
}

class NavigatorState extends State<MyNavigatorPage> {
  final String title;

  NavigatorState({this.title = '导航页'});

  @override
  void initState() {
    super.initState();
    print("title:$title");
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      alignment: Alignment.center,
      color: Colors.white,
      child: new TextButton(
        onPressed: () {
          _jumpPage(context);
        },
        child: new Text('跳转'),
      ),
    );
  }

  _jumpPage(BuildContext context) {
    print("_jumpPage");

    // Navigator.of(context)
    //     .push(MaterialPageRoute(builder: (context) => new MyNavPage3())).then((value) => print("返回的数据：$value"));
    // Navigator.push(context, new MaterialPageRoute(builder: (context) => new MyNavPage1()));
    // Navigator.push(
    //     context,
    //     MaterialPageRoute(
    //         builder: (context) => MyNavPage2(title: "传过来的第二个页面")));
    ///跳转动画
    Navigator.push(
        context,
        PageRouteBuilder(
          pageBuilder: (context, animation, secondaryAnimation) => MyNavPage1(),
          transitionsBuilder: (context, animation, secondaryAnimation, child) {
            var begin = Offset(0.0, 1.0);

            ///
            var end = Offset.zero;

            ///恢复原始位置
            var curve = Curves.ease;

            ///先快后慢

            var tween =
                Tween(begin: begin, end: end).chain(CurveTween(curve: curve));
            return SlideTransition(
              position: animation.drive(tween),
              child: child,
            );
          },
        ));

    ///iOS跳转
    // Navigator.push(context, CupertinoPageRoute(builder: (context) => new MyNavPage1()));

    ///fluro导航
    NavigatorUtils.push(context,
        "/login/loginPage?bundle_key_user_name=admin&bundle_key_password=123456");
  }
}

class MyNavPage1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Container(
      alignment: Alignment.center,
      color: Colors.blue,
      child: TextButton(
        child: Text("返回第一个页面"),
        onPressed: () {
          _jumpPage2(context);
        },
      ),
    );
  }

  _jumpPage2(BuildContext context) {
    Navigator.pop(
        context, MaterialPageRoute(builder: (context) => MyNavigatorPage()));
  }
}

class MyNavPage2 extends StatelessWidget {
  final String title;

  MyNavPage2({this.title = "第二个页面"});

  ///Key不知道是干啥的？？？
  // MyNavPage2({Key key, this.title = "第二个页面"});

  @override
  Widget build(BuildContext context) {
    return new Container(
      alignment: Alignment.center,
      color: Colors.red,
      child: new Text(
        title,
        style: TextStyle(
            color: Colors.white, fontSize: 28, decoration: TextDecoration.none),
      ),
    );
  }
}

class MyNavPage3 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MyNavPageState();
}

class _MyNavPageState extends State<MyNavPage3> {
  @override
  Widget build(BuildContext context) {
    return new Container(
      alignment: Alignment.center,
      color: Colors.yellow,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          TextButton(
            child: Text('点击跳转页面二'),
            onPressed: () {
              _jumpNext(context);
            },
          ),
          TextButton(
            child: Text('返回'),
            onPressed: () {
              _backPrev(context);
            },
          )
        ],
      ),
    );
  }

  _jumpNext(BuildContext context) {
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (context) => new MyNavPage4()));
  }

  _backPrev(BuildContext context) {
    Navigator.of(context).pop("页面三的数据");
  }
}

//第二个页面
class MyNavPage4 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('页面四'),
      ),
      body: Center(
        child: TextButton(
          child: Text("返回"),
          onPressed: () {
            Navigator.pop(
              context,
              MaterialPageRoute(builder: (context) => MyNavigatorPage()),
            );
          },
        ),
      ),
    );
  }
}

///登录页面
class MyNavLoginPage extends StatefulWidget {
  static String bundleKeyUserName = "bundle_key_user_name";
  static String bundleKeyPassword = "bundle_key_password";

  String username;
  String password;

  MyNavLoginPage(this.username, this.password) {
    print("username:$username, password:$password");
  }

  @override
  State<StatefulWidget> createState() => _MyNavLoginState();
}

class _MyNavLoginState extends State<MyNavLoginPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      alignment: Alignment.center,
      child: Text("登录用户名:${widget.username}, 密码:${widget.password}"),
    );
  }
}

///页面找不到
class MyNavNotFoundPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyNavNotFoundPage();
}

class _MyNavNotFoundPage extends State<MyNavNotFoundPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.yellow,
      alignment: Alignment.center,
      child: Text("页面找不到..."),
    );
  }
}
